package com.starhub.api.docarchive.controller;

import com.starhub.api.docarchive.dto.DocArchiveBaseDTO;
import com.starhub.api.docarchive.result.DocArchiveDealResult;
import com.starhub.api.docarchive.result.DocArchiveListResult;
import com.starhub.api.docarchive.service.DocArchiveService;
import com.starhub.common.bean.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**   
 * 系统知识库管理API接口层
 * @version: V1.0
 * @author: liuhf
 * @time 2025-05-18
 */
@Api(tags = "系统知识库管理")
@Slf4j
@RestController
@RequestMapping("/api/starhub/docArchive")
public class DocArchiveController {

    @Autowired
    private DocArchiveService docArchiveService;

    @ApiOperation("获取系统知识库列表")
    @GetMapping("/shared/list")
    public ResultResp<DocArchiveListResult> getSharedDocArchiveList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return ResultResp.success(docArchiveService.getSharedDocArchiveList(pageNum, pageSize));
        } catch (Exception e) {
            log.error("获取系统知识库列表异常", e);
            return ResultResp.fail("获取系统知识库列表异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("查询知识库列表")
    @GetMapping("/search/list")
    public ResultResp<DocArchiveListResult> getSearchDocArchiveList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String keyWord,
            @RequestParam(defaultValue = "1") Integer type) {
        try {
            return ResultResp.success(docArchiveService.getSearchDocArchiveList(pageNum, pageSize,keyWord,type));
        } catch (Exception e) {
            log.error("查询知识库列表异常", e);
            return ResultResp.fail("查询知识库列表异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("获取个人知识库列表")
    @GetMapping("/personal/list")
    public ResultResp<DocArchiveListResult> getPersonalDocArchiveList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return ResultResp.success(docArchiveService.getPersonalDocArchiveList(pageNum, pageSize));
        } catch (Exception e) {
            log.error("获取个人知识库列表异常", e);
            return ResultResp.fail("获取个人知识库列表异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("获取已关注的知识库列表")
    @GetMapping("/followed/list")
    public ResultResp<DocArchiveListResult> getFollowedArchiveList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return ResultResp.success(docArchiveService.getFollowedArchiveList(pageNum, pageSize));
        } catch (Exception e) {
            log.error("获取已关注知识库列表异常", e);
            return ResultResp.fail("获取已关注知识库列表异常：" + e.getMessage(),e);
        }
    }
    
    @ApiOperation("分享到系统知识库")
    @PutMapping("/share/{id}")
    public ResultResp<DocArchiveDealResult> shareDocArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.checkShareDocArchive(id, 1));
        } catch (Exception e) {
            log.error("分享知识库异常 {}", id, e);
            return ResultResp.fail("分享知识库异常：" + e.getMessage() ,e);
        }
    }

    @ApiOperation("取消分享")
    @PutMapping("/unshare/{id}")
    public ResultResp<DocArchiveDealResult> unshareDocArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.checkShareDocArchive(id, 0));
        } catch (Exception e) {
            log.error("取消分享知识库异常 {}", id, e);
            return ResultResp.fail("取消分享知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("查询知识库详情")
    @PutMapping("/query/{id}")
    public ResultResp<DocArchiveDealResult> getDocArchiveById(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.getDocArchiveById(id));
        } catch (Exception e) {
            log.error("取消分享知识库异常 {}", id, e);
            return ResultResp.fail("取消分享知识库异常：" + e.getMessage(),e);
        }
    }
    
    @ApiOperation("新建知识库")
    @PostMapping("/create")
    public ResultResp<DocArchiveDealResult> createDocArchive(@RequestBody DocArchiveBaseDTO dto) {
        try {
            return ResultResp.success(docArchiveService.createDocArchive(dto));
        } catch (Exception e) {
            log.error("新建知识库异常", e);
            return ResultResp.fail("新建知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("编辑知识库")
    @PutMapping("/edit")
    public ResultResp<DocArchiveDealResult> updateDocArchive(@RequestBody DocArchiveBaseDTO dto) {
        try {
            return ResultResp.success(docArchiveService.updateDocArchive(dto));
        } catch (Exception e) {
            log.error("编辑知识库异常 {}", dto.getId(), e);
            return ResultResp.fail("编辑知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("删除知识库")
    @DeleteMapping("/delete/{id}")
    public ResultResp<DocArchiveDealResult> deleteDocArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.deleteDocArchive(id));
        } catch (Exception e) {
            log.error("删除知识库异常 {}", id, e);
            return ResultResp.fail("删除知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("关注知识库")
    @PostMapping("/follow/{id}")
    public ResultResp<DocArchiveDealResult> followArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.followArchive(id));
        } catch (Exception e) {
            log.error("关注知识库异常 {}", id, e);
            return ResultResp.fail("关注知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("取消关注知识库")
    @DeleteMapping("/unfollow/{id}")
    public ResultResp<DocArchiveDealResult> unfollowArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.unfollowArchive(id));
        } catch (Exception e) {
            log.error("取消关注知识库异常 {}", id, e);
            return ResultResp.fail("取消关注知识库异常：" + e.getMessage(),e);
        }
    }

    @ApiOperation("检查是否已关注")
    @GetMapping("/checkfollow/{id}")
    public ResultResp<Boolean> checkFollowed(@PathVariable Long id) {
        try {
            return ResultResp.success(docArchiveService.isFollowed(id));
        } catch (Exception e) {
            log.error("检查关注状态异常 {}", id, e);
            return ResultResp.fail("检查关注状态异常：" + e.getMessage(),e);
        }
    }
} 